{template 'web/'.$cfg['floder'].'/base_menu'}
<script src="{MODULE_URL}template/resources/js/jquery-ui.min.js"></script>
<div class="top_tab">
    <a href="{php echo $this->createWebUrl('admanage',array('op'=>'display'))}" class="layui-btn {if $operation!='display'}layui-btn-primary{/if}">广告管理</a>
    <span class="help">注:按住拖放可改变排序</span>
</div>
<style>
    .panelbox-head i{ color:#009688;}
    ul.pcate li{ padding:10px; margin-bottom:1px; cursor:pointer}
    ul.pcate li i{ color:#999}
    ul.pcate li.isselect { color:#FFF; background:#7266ba}
    ul.pcate li.isselect i{ color:#f58a20}
    ul.pcate li:hover{color:#FFF; background:#7266ba}
    ul.pcate li a{color:#FFF; margin:0 10px;display:none;}
    ul.pcate li a i{color:#FFF;}
    ul.pcate li.isselect a{display:inline-block;}
    ul.pcate li:hover a{ display:inline-block;}
    ul.pcate li.isselect a i,ul.pcate li:hover a i{color:#FFF;}
    ul.listtable{-webkit-display:flex;display:flex;-webkit-flex-wrap:wrap;flex-wrap:wrap;}
    ul.listtable li{border:1px solid #DDD;padding:10px; width:24%; margin:5px 0.5%; position:relative; vertical-align:-5px; background:#FFF; cursor:pointer}

    ul.listtable li .t_menu{line-height:18px;height: 18px;}
    ul.listtable li .t_menu span{float: right;}
    ul.listtable li .t_menu a{ display:inline-block; margin:5px 10px}
    ul.listtable li h2{ font-size:20px; line-height:20px; margin:10px 0; padding:0;text-align:center;}
    ul.listtable li .b_menu{ line-height:18px;text-align:left;}

</style>
{if $operation == 'display'}
<div class="row">
    <div class="col-md-3">
        <div class="panelbox">
            <form class="layui-form" onsubmit="return false;" action="">
                <div class="panelbox-head form-inline">
                    <A href="#" style="margin-top:5px; display:inline-block">店铺管理</A>

                </div>
            </form>
            <div class="panelbox-body">
                <ul class="pcate">
                    {if $_W['isfounder'] || $_W['role']=="manager"}
                    <li cid="0" onclick="chagngepcate(this)"><i class="fa fa-bookmark"></i> <b>公共</b> <span class="pull-right"><i class="fa fa-menu-right"></i> </span></li>
                    {loop $shoplist $row}
                    <li cid="{$row['id']}" onclick="chagngepcate(this)"><i class="fa fa-bookmark"></i> <b>{$row['companyname']}</b> <span class="pull-right"><i class="fa fa-menu-right"></i> </span></li>
                    {/loop}
                    {else}
                    {if $shopid}
                    <li cid="{php echo $shopid}" onclick="chagngepcate(this)"><i class="fa fa-bookmark"></i> <b>{$item['companyname']}</b> <span class="pull-right"><i class="fa fa-menu-right"></i> </span></li>
                    {/if}
                    {/if}
                </ul>

            </div>
        </div>
    </div>
    <div class="col-md-9">
        <div class="panelbox">
            <div class="panelbox-head"> <A href="#">广告</A> <A href="{php echo $this->createWebUrl('admanage',array('op'=>'post'))}"  class="btn btn-success">新增</A> </div>
            <div class="panelbox-body">
                <ul class="listtable">

                </ul>
            </div>
        </div>
    </div>
</div>


<script language="javascript">
    $(document).ready(function(e) {
        $(".pcate li:eq(0)").click();
        $(".listtable").sortable({delay:300,update:function(){
            updateMove();
        }});
    });
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        var myDate = new Date();
        var datenow=myDate.toLocaleDateString();
        laydate.render({
            elem: '#range_s'
        });
        laydate.render({
            elem: '#range_e'
        });
    });
    layui.use(['layer', 'laypage', 'element','form'], function(){
        var layer = layui.layer
                ,laypage = layui.laypage
                ,element = layui.element
                ,form = layui.form;

        form.on('submit(btnsubmit)', function(data){
            if(data.field.pcate==""){
                layer.msg("请选择店铺",{icon:2,time:2000});
                return false;
            }
            console.log(JSON.stringify(data.field));
            var index2 =layer.load(1, {shade: [0.1,'#fff']});
            $.post("{php echo $this->createWebUrl('admanage',array('op'=>'posttable'))}",data.field,function(returnstr){
                layer.close(index2);
                console.log(returnstr);
                var result=eval("("+returnstr+")");
                if(result.success){
                    layer.close($("#editcateform").attr("index"));
                    var items=result.item;
                    $(".pcate li[cid='"+items.groupid+"']").removeClass("isselect").click();
                }else{
                    layer.msg(result.msg,{icon:2,time:2000});
                    return false;
                }
            });
            return false;
        });

    });

    function chagngepcate(obj){
        //if($(obj).hasClass("isselect"))return;
        $(obj).addClass("isselect").siblings().removeClass("isselect");
        var pcate=$(obj).attr("cid");
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        $(".listtable").empty();
        $.post("{php echo $this->createWebUrl('admanage',array('op'=>'getpcatetable'))}",{"pcate":pcate},function(returnstr){
            layer.close(index2);
            console.log(returnstr);
            var result=eval("("+returnstr+")");
            if(!result.success){
                layer.msg(result.msg,{icon:2,time:2000});
                return;
            }
            var list=result.list;
            var temp="";
            for(var i in list){
                temp+='<li tid="'+list[i].id+'"><div class="t_menu"><span><a href="{php echo $this->createWebUrl("admanage",array("op"=>"post"));}&id='+list[i].id+'"><i class="fa fa-edit"></i></a> <a href="javascript:" onClick="deleteContent(\'{php echo $this->createWebUrl("admanage",array("op"=>"deletetable"))}&id='+list[i].id+'\')"><i class="fa fa-trash"></i></a></span>'+list[i].title+'</div><img src="'+list[i].thumb+'" onerror="this.src=\'./resource/images/nopic.jpg\'" title="'+list[i].title+'" style="width: 100%; height: 120px;"/></div></li>';
            }
            $(".listtable").html(temp);
        });
    }

    function deleteContent(url){
        layer.confirm('确认删除此内容？删除后将不可恢复', {}, function(){
            location.href=url;
        });
    }

    function updateMove(){
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        var ary=[];
        $(".listtable li").each(function(index, element) {
            ary.push($(this).attr("tid"));
        });
        $.post("{php echo $this->createWebUrl('admanage',array('op'=>'resettableorder'))}",{"displayorder":ary.join(",")},function(data){
            layer.close(index2);
            if(!data.success){
                layer.msg(data.msg,{icon:2,time:2000});
                return;
            }

        },'json');
    }

</script>

{elseif $operation == 'post'}
<div class="panelbox">
    <form action="" method="post" class="layui-form" enctype="multipart/form-data">
        <input type="hidden" name="id" value="{php echo $id}" />
        <div class="panelbox-head"> {if !$id}添加{else}编辑{/if} </div>
        <div class="panelbox-body">
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" lay-verify="required" name="title" value="{$item['title']}"/>
                    <div class="help-block">标题</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">开关</label>
                <div class="layui-input-block">
                    <input type="checkbox" {if $item['status']==1}checked{/if} name="status"  value="1" lay-skin="switch" lay-text="启用|关闭"/>
                    <div class="help-block">活动开关</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属店铺</label>
                <div class="layui-input-block form-inline">
                    <select name="pcate" class="layui-input">
                        {if $_W['isfounder'] || $_W['role']=="manager"}
                        <option value="0">公共</option>
                        {loop $shoplist $row}
                        <option value="{$row['id']}">{$row['companyname']}</option>
                        {/loop}
                        {else}
                        {if $shopid}
                        <option value="{php echo $shopid}">{$shop['companyname']}</option>
                        {/if}
                        {/if}
                    </select>
                    <div class="help-block"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">时间</label>
                <div class="layui-input-block">
                    {php echo tpl_form_field_daterange('gametime', array('start' => date('Y-m-d', $item['starttime']),'end'=> date('Y-m-d', $item['endtime'])))}
                    <div class="help-block">活动时间</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">活动图片</label>
                <div class="layui-input-block">
                    {php echo tpl_form_field_image('thumb', $item['thumb']);}
                    <div class="help-block">图片为背景</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                    {php echo tpl_ueditor('info', $item['info']);}
                    <div class="help-block">可以查视频等内容</div>
                </div>
            </div>

            <div class="panelbox-foot">
                <input type="hidden" name="token" value="{$_W['token']}" />
                <input class="layui-btn" type="submit" lay-submit="" name="submit" lay-filter="btnsmbmit" value="提交">
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['layer', 'laypage', 'element','form'], function() {
        var layer = layui.layer
                , laypage = layui.laypage
                , element = layui.element
                , form = layui.form;
        form.render();
    });
</script>
{/if}